﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using eeenno.ReaderEngine.Adapters;

namespace eeenno.ReaderEngine.Utilities
{
    public class Settings
    {
        //private static string _default_browser = String.Empty;
        private static DateTime _unix_date = new DateTime(1970, 1, 1, 0, 0, 0);
        private static ConfigurationManager _cm = null;

        public static string DumpSettings()
        {
            return _cm.DumpSettings();
        }

        public static bool Initialize()
        {
            _cm = new ConfigurationManager();

            /*initialize the default browser
            try
            {
                string temp = _cm.ReadClassesRootKey("Default", "\\http\\Shell\\Open\\Command");
                _default_browser = temp.Remove(temp.LastIndexOf(" "), temp.Length - temp.LastIndexOf(' '));
            }
            catch (Exception e)
            {
                _default_browser = "iexplore.exe";
            }*/

            return _cm.ReadINIFile();
        }

        public static bool WriteSettings()
        {
            return _cm.WriteINIFile();
        }

        public static DateTime unix_datetime
        {
            get { return _unix_date; }
        }

        public static string username
        {
            get { return _cm.GetSetting("Username"); }
            set { _cm.SetSetting("Username",value); }            
        }

        public static string password
        {
            get 
            {
                string temp = _cm.GetSetting("Password");
                if (temp.Length > 0)
                    return Security.Decrypt(temp);
                else
                    return temp;
            }
            set { _cm.SetSetting("Password", Security.Encrypt(value)); }
        }

        public static int download_size
        {
            get { return _cm.GetIntSetting("DownloadSize"); }
            set { _cm.SetIntSetting("DownloadSize", value); }
        }

        public static string Key
        {
            get { return _cm.GetSetting("Key"); }
            set { _cm.SetSetting("Key", value); }
        }

        public static string Four
        {
            get { return _cm.GetSetting("Four"); }
            set { _cm.SetSetting("Four", value); }
        }

        public static int refresh_duration
        {
            get { return _cm.GetIntSetting("RefreshDuration"); }
            set { _cm.SetIntSetting("RefreshDuration", value); }
        }  

        public static bool hide_read_items
        {
            get { return _cm.GetBoolSetting("HideReadItems"); }
            set { _cm.SetBoolSetting("HideReadItems", value); }
        }

        public static bool auto_login
        {
            get { return _cm.GetBoolSetting("AutoLogin"); }
            set { _cm.SetBoolSetting("AutoLogin", value); }
        }

        public static bool show_reading_list_at_startup
        {
            get { return _cm.GetBoolSetting("ShowReadingListAtStartup"); }
            set { _cm.SetBoolSetting("ShowReadingListAtStartup", value); }
        }

        /*public static string default_browser()
        {
            return _default_browser;
        }*/

        public static int page_size
        {
            get { return _cm.GetIntSetting("PageSize"); }
            set { _cm.SetIntSetting("PageSize", value); }
        }

        public static bool always_open_in_browser
        {
            get { return _cm.GetBoolSetting("AlwaysOpenInBrowser"); }
            set { _cm.SetBoolSetting("AlwaysOpenInBrowser", value); }
        }       

        public static string twitter_username
        {
            get { return _cm.GetSetting("TwitterUsername"); }
            set { _cm.SetSetting("TwitterUsername", value); }
        }

        public static string twitter_password
        {
            get
            {
                string temp = _cm.GetSetting("TwitterPassword");
                if (temp.Length > 0)
                    return Security.Decrypt(temp);
                else
                    return temp;
            }
            set { _cm.SetSetting("TwitterPassword", Security.Encrypt(value)); }
        }
        
        public static bool shorten_url
        {
            get { return _cm.GetBoolSetting("ShortenURL"); }
            set { _cm.SetBoolSetting("ShortenURL", value); }
        }

        public static bool download_images
        {
            get { return _cm.GetBoolSetting("DownloadImages"); }
            set { _cm.SetBoolSetting("DownloadImages", value); }
        }

        public static bool data_while_roaming_enabled
        {
            get { return _cm.GetBoolSetting("EnableDataWhileRoaming"); }
            set { _cm.SetBoolSetting("EnableDataWhileRoaming", value); }
        }

        public static bool caching_enabled
        {
            get { return _cm.GetBoolSetting("EnableCaching"); }
            set { _cm.SetBoolSetting("EnableCaching", value); }
        }

        public static bool store_cache_on_memory_card
        {
            get { return _cm.GetBoolSetting("StoreCacheOnMemoryCard"); }
            set { _cm.SetBoolSetting("StoreCacheOnMemoryCard", value); }
        }

        public static bool logging_enabled
        {
            get { return _cm.GetBoolSetting("EnableLogging"); }
            set { _cm.SetBoolSetting("EnableLogging", value); }
        }

        public static bool offline_mode_enabled
        {
            get { return _cm.GetBoolSetting("OfflineMode"); }
            set { _cm.SetBoolSetting("OfflineMode", value); }
        }

        public static int max_articles_to_cache
        {
            get { return _cm.GetIntSetting("MaximumArticlesToCache"); }
            set { _cm.SetIntSetting("MaximumArticlesToCache", value); }
        }

        public static int item_size
        {
            get { return _cm.GetIntSetting("ItemSize"); }
            set { _cm.SetIntSetting("ItemSize", value); }
        }

        public static bool show_progress_bar
        {
            get { return _cm.GetBoolSetting("ShowProgressBar"); }
            set { _cm.SetBoolSetting("ShowProgressBar", value); }
        }

        public static bool show_icons
        {
            get { return _cm.GetBoolSetting("ShowIcons"); }
            set { _cm.SetBoolSetting("ShowIcons", value); }
        }

        public static int search_results_size
        {
            get { return _cm.GetIntSetting("SearchResultsSize"); }
            set { _cm.SetIntSetting("SearchResultsSize", value); }
        }

        public static int theme
        {
            get { return _cm.GetIntSetting("Theme"); }
            set { _cm.SetIntSetting("Theme", value); }
        }

        public static bool first_run
        {
            get { return _cm.GetBoolSetting("FirstRun"); }
            set { _cm.SetBoolSetting("FirstRun", value); }
        }

        public static bool always_use_internal_browser
        {
            get { return _cm.GetBoolSetting("AlwaysUseInternalBrowser"); }
            set { _cm.SetBoolSetting("AlwaysUseInternalBrowser", value); }
        }

        public static bool use_smaller_list_font
        {
            get { return _cm.GetBoolSetting("UseSmallerListFont"); }
            set { _cm.SetBoolSetting("UseSmallerListFont", value); }
        }

        public static string application_directory
        {
            get
            {
                return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.
                    GetExecutingAssembly().GetName().CodeBase) + "\\";
            }
        }

        public static bool check_for_update_at_login
        {
            get { return _cm.GetBoolSetting("CheckForUpdateAtLogin"); }
            set { _cm.SetBoolSetting("CheckForUpdateAtLogin", value); }
        }

        public static string registration_key
        {
            get { return _cm.GetSetting("RegistrationKey"); }
            set { _cm.SetSetting("RegistrationKey", value); }
        }

        public static long toi
        {
            get { return _cm.GetLongSetting("Toi"); }
            set { _cm.SetLongSetting("Toi", value); }
        }

        public static string cache_path
        {
            get
            {
                if (store_cache_on_memory_card && CacheEngine.storage_card_path.Length > 0)
                {
                    return CacheEngine.storage_card_path + "\\SpeeedReaderCache\\";                    
                }
                else
                {
                    return application_directory + "SpeeedReaderCache\\";
                }
            }
        }
    }
}
